/**
 * 随机UUID
 */

let uid = 1;
export const useUnique = () => {
  // eslint-disable-next-line no-plusplus
  return Date.now() + uid++;
};
/**
 * 统计字数和预计阅读时间
 * @param content
 */
export const useCalculateReadability = (content: string) => {
  // 统计字数
  const words = content.split(/\s+/).filter(word => word.length > 0);
  const wordCount = words.length;
  // 使用 k 单位转换字数
  let formattedWordCount = '';
  if (wordCount >= 1000) {
    const kCount = Math.ceil((wordCount / 1000)).toFixed(3);
    formattedWordCount = `${kCount}k`;
  } else {
    formattedWordCount = wordCount.toString();
  }
  // 预计阅读时间
  const averageReadingSpeed = 200; // 每分钟阅读200个字
  const readingTimeMinutes = Math.ceil(wordCount / averageReadingSpeed);
  // 将分钟转换为可读的格式（例如：x 分钟）
  const readingTime = readingTimeMinutes > 1 ? `${readingTimeMinutes} 分钟` : '1 分钟';
  return { wordCount: formattedWordCount, readingTime };
};

/**
 *
 * @param count
 */
export const useCountTransform = (count: number) => {
  const _count = +count;
  if (_count >= 1000 && _count < 10000) {
    const kCount = Math.ceil((_count / 1000)).toFixed(2);
    return `${kCount}k`;
  }
  if (_count >= 10000) {
    const tCount = Math.ceil((_count / 10000)).toFixed(3);
    return `${tCount}w`;
  }
  return _count;
};

/**
 * 检验对象是否为空
 * @param obj
 */
export const useIsEmptyObject = (obj: Object | undefined) => {
  if (!obj) {
    return true;
  }
  return Object.keys(obj).length === 0;
};

/**
 * 获取当前时间是哪个时间段
 */
export const getCurrentTimePeriod = () => {
  const currentHour = new Date().getHours();

  if (currentHour >= 5 && currentHour < 12) {
    return '早上好';
  }
  if (currentHour >= 12 && currentHour < 18) {
    return '中午好';
  }
  return '晚上好';
};
/**
 * 横杠转驼峰
 * @param kebabCaseName
 */
export const useToCamelCased = (kebabCaseName: string) => {
  const hyphenateRE = /([a-z])-([a-z])/ig;
  return kebabCaseName
    .replace(hyphenateRE, (str: string, $1: string, $2: string) => `${$1}${$2.toUpperCase()}`);
};
/**
 * 时间日期随机标识
 */
export const useGenerateIdentifier = () => {
  // 获取当前时间
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const date = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  const seconds = String(now.getSeconds()).padStart(2, '0');

  // 生成一个随机数
  const randomNumber = Math.floor(Math.random() * 100); // 生成0-99之间的随机数

  // 将时间各部分和随机数拼接成一个字符串作为标识符
  return `${year}${month}${date}${hours}${minutes}${seconds}${randomNumber}`;
};
// 检测是否为移动端
export const isMobile = useMediaQuery(
  '(max-width: 767px) and (orientation: portrait), '
  + '(max-width: 1023px) and (orientation: landscape)'
);
